package com.lesso.zgs.sunflower.domain.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.zgs.sunflower.domain.repository.EmployeeDeviceRepository;
import com.lesso.zgs.sunflower.model.EmployeeDevice;
import com.lesso.zgs.sunflower.pojo.dto.EmployeeDeviceDTO;
import com.lesso.zgs.sunflower.pojo.dto.EmployeeDeviceQueryDTO;
import com.lesso.zgs.sunflower.pojo.vo.EmployeeDeviceVO;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * EmployeeDeviceDomainService
 *
 * @author zgs
 * @since 2025-07-03
 */
@Service
public class EmployeeDeviceDomainService {
    private final EmployeeDeviceRepository employeeDeviceRepository;

    public EmployeeDeviceDomainService(EmployeeDeviceRepository employeeDeviceRepository) {
        this.employeeDeviceRepository = employeeDeviceRepository;
    }

    public Integer authorizeDevice(EmployeeDeviceDTO dto) {
        EmployeeDevice employeeDevice = new EmployeeDevice();
        employeeDevice.setEmployeeId(dto.getEmployeeId());
        employeeDevice.setDeviceCode(dto.getDeviceCode());
        employeeDevice.setAuthorizationDate(dto.getAuthorizationDate() != null ? dto.getAuthorizationDate() : LocalDate.now());
        employeeDevice.setAuthorizationStatus(dto.getAuthorizationStatus() != null ? dto.getAuthorizationStatus() : 1);
        return employeeDeviceRepository.save(employeeDevice);
    }

    public Integer cancelAuthorization(Long id) {
        return employeeDeviceRepository.delete(id);
    }

    public Page<EmployeeDeviceVO> page(EmployeeDeviceQueryDTO dto) {
        return employeeDeviceRepository.getPageList(dto);
    }

    public List<EmployeeDeviceVO> getByEmployeeId(Long employeeId) {
        return employeeDeviceRepository.getByEmployeeId(employeeId);
    }
} 